//
//  SocketClientManager.swift
//  QHSocketIODemo
//
//  Created by Anakin chen on 2017/5/22.
//  Copyright © 2017年 chen. All rights reserved.
//

import UIKit

class SocketClientManager: NSObject {
    open static let sharedManager = SocketClientManager()
    
    private var sockets = [String: SocketIOClient]()
    
    open subscript(string: String) -> SocketIOClient? {
        get {
            return sockets[string]
        }
        
        set(socket) {
            sockets[string] = socket
        }
    }
    
    open func addSocket(_ socket: SocketIOClient, labeledAs label: String) {
        sockets[label] = socket
    }
    
    open func removeSocket(withLabel label: String) -> SocketIOClient? {
        return sockets.removeValue(forKey: label)
    }
    
    open func removeSocket(_ socket: SocketIOClient) -> SocketIOClient? {
        var returnSocket: SocketIOClient?
        
        for (label, dictSocket) in sockets where dictSocket === socket {
            returnSocket = sockets.removeValue(forKey: label)
        }
        
        return returnSocket
    }
    
    open func removeSockets() {
        sockets.removeAll()
    }
}
